package com.smartcruise.main.contactsFragment;

import android.view.View;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.smartcruise.R;
import com.smartcruise.base.BaseTitleFragment;
import com.smartcruise.base.FragmentActivitySimpleImp;
import com.smartcruise.base.RefreshContract;
import com.smartcruise.base.RefreshSimpleImp;
import com.smartcruise.base.TitleBuilder;

import butterknife.BindView;

/**
 * Created by FengChaoQun
 * on 2018/1/20 0020
 * 通讯录
 */

public class ContactsFragment extends BaseTitleFragment implements RefreshContract.View {
    @BindView(R.id.listView)
    ListView listView;
    @BindView(R.id.swipeRefreshLayout)
    SmartRefreshLayout swipeRefreshLayout;
    @BindView(R.id.emptyView)
    TextView emptyView;

    private RefreshContract.Presenter mPresenter;
    protected RefreshContract.RefreshInterface refreshInterface;

    @Override
    protected int getContentLayout() {
        return R.layout.util_refresh_list;
    }

    @Override
    protected TitleBuilder getTitleBuilder() {
        return TitleBuilder.create(false, getString(R.string.contacts));
    }

    @Override
    protected void init() {
        setEnableRightSlide(false);
        refreshInterface = new RefreshSimpleImp(swipeRefreshLayout);
        refreshInterface.setOnRefreshListener(new RefreshContract.OnRefreshListener() {
            @Override
            public void onRefresh(RefreshContract.RefreshInterface refreshInterface) {
                mPresenter.refresh();
            }
        });
        refreshInterface.setOnLoadMoreListener(new RefreshContract.OnLoadMoreListener() {
            @Override
            public void onLoadMore(RefreshContract.RefreshInterface refreshInterface) {
                mPresenter.loadMore();
            }
        });
        mPresenter = new ContactsPresenterImp(this);
        mPresenter.start();

        View headView = View.inflate(getContext(), R.layout.util_serach_head, null);
        listView.addHeaderView(headView);
        listView.setEmptyView(emptyView);
        headView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                FragmentActivitySimpleImp.start(getActivity(), SearchContactsFragment.class);
            }
        });
    }

    @Override
    public void onDestroyView() {
        mPresenter.destroy();
        super.onDestroyView();
    }

    /**
     * description:获取刷新组件
     */
    @Override
    public RefreshContract.RefreshInterface getRefresh() {
        return refreshInterface;
    }

    @Override
    public void setData(BaseAdapter baseAdapter) {
        listView.setAdapter(baseAdapter);
    }

}
